package com.hawkstar66.practice.funny.executors;

import java.util.Scanner;

import com.hawkstar66.practice.funny.Executor;
import com.hawkstar66.practice.funny.commons.FunnyUtils;

public class MonkeyEatPeach implements Executor {

	public String getStory() {
		StringBuffer story = new StringBuffer();
		story.append("一只猴子到处闲逛，突然发现一堆桃子，它赶紧把桃子全部弄回家。").append(FunnyUtils.ENTER);
		story.append("第一天猴子忍不住桃子诱惑，吃了所有桃子的一半数量，").append(FunnyUtils.ENTER);
		story.append("吃完了一半后，猴子忍不住又拿起桃子一个吃了，这才挨到第二天。").append(FunnyUtils.ENTER);
		story.append("第二天猴子又吃了前一天所有剩余桃子的一半加1个，依次类推，").append(FunnyUtils.ENTER);
		story.append("直到第10(n)天，猴子还只剩1(y)个桃子。请问猴子第一天总共捡到多少桃子？").append(FunnyUtils.ENTER);
		return story.toString();
	}

	public String getExecutorExplain() {
		return "猴子吃桃[天数,总天数,最后一天桃子数]";
	}

	public void execute(Scanner input) {
		System.out.println(FunnyUtils.makeInputExplain("天数(计算哪一天的桃子数)"));
		long day = input.nextLong();
		if(day <= 0) {
			System.out.println("天数大于0");
			return;
		}
		
		System.out.println(FunnyUtils.makeInputExplain("总天数"));
		long totalDay = input.nextLong();
		if(day > totalDay) {
			System.out.println("总天数 必须大于 天数");
			return;
		}
		
		System.out.println(FunnyUtils.makeInputExplain("最后一天桃子数"));
		long peachNumberOfLastDay = input.nextLong();
		if(peachNumberOfLastDay < 0) {
			System.out.println("桃子数 必须大于或等于 0");
			return;
		}
		
		System.out.println(FunnyUtils.makeInputExplain("有2种算法，选择一种算法。0.全部算法；1.递归算法；2.for循环算法"));
		int order = input.nextInt();
		
		if(order < 0 || order > 2 ) {
			return;
		}
		if(0 == order || 1 == order) {
			FunnyUtils.printAlgorithmExplain(1, "递归算法");
			print(day, getPeachNumber(day, totalDay, peachNumberOfLastDay));
		} 
		
		if(0 == order || 2 == order) {
			FunnyUtils.printAlgorithmExplain(1, "for循环算法");
			print(totalDay, getPeachNumber1(day, totalDay, peachNumberOfLastDay));
		}
	}
	
	private void print(long day, long peachNumber) {
		System.out.println("第" + day + "天吃桃前有" + peachNumber + "个桃子");
	}
	
	/**
	 * 实现方式1
	 * @param day
	 * @param totalDay
	 * @param peachNumberOfLastDay
	 * @return
	 */
	public long getPeachNumber(long day, long totalDay, long peachNumberOfLastDay) {
		if(day == totalDay) {
			return peachNumberOfLastDay;
		} else {
			return 2 * (getPeachNumber(day+1L, totalDay, peachNumberOfLastDay) + 1L);
		}
	}
	
	/**
	 * 实现方式2
	 * @param day 第几天，要计算桃子数的天
	 * @param totalDay 总天数
	 * @param peachNumberOfLastDay 最后一天桃子数
	 * @return
	 */
	public long getPeachNumber1(long day, long totalDay, long peachNumberOfLastDay) {
		long peachNumber = 0;
		for(long i = totalDay; i > 0; i--) {
			if(i == totalDay) {
				peachNumber = peachNumberOfLastDay;
			} else {
				peachNumber = 2 * (peachNumber + 1);
			}
			if(i == day) {
				break;
			}
		}
		return peachNumber;
	}

	public static void main(String[] args) {
		
		
		
		MonkeyEatPeach mep = new MonkeyEatPeach();
		System.out.println(mep.getPeachNumber(1, 10, 1));
		System.out.println(mep.getPeachNumber1(1, 10, 1));
	}
}
